#include "tcpconnection.h"

TcpConnection::TcpConnection(QObject *parent):QObject(parent)
{
    _server = new TcpServer;
    connect(_server, SIGNAL(receivedMessage(QByteArray)),
        this, SLOT(receivedMessage(QByteArray)));

    connect(_server, SIGNAL(acceptedConnection()),
            this, SLOT(acceptConnection()));



}
TcpConnection::~TcpConnection()
{
    if(_server!=NULL)
        delete _server;
}

void TcpConnection::receivedMessage(QByteArray message)
{
    qDebug()<<"TcpConnection::receivedMessage(QByteArray message)";
    QByteArray msgCode=Message::getMessageMSG(message);
    qDebug()<<"MSG as hex"<<msgCode.toHex();
    switch(msgCode.at(0))
    {
    case Message::MSG_START:
        qDebug()<<"emit receivedStart(Message::getMessageText(message))";
        //emit receivedStart(Message::getMessageText(message));
        emit receivedStart(Message::getMessageDiscData(message));
        break;
    case Message::MSG_STOP:
        qDebug()<<"emit receivedStop()";
        emit receivedStop();
        break;
    case Message::MSG_SEARCH_MOVIE_DATA:
        qDebug()<<"emit receivedSearchMovieData()";
        emit receivedSearchMovieData(Message::getMessageMovieData(message));
        break;
    case Message::MSG_SEARCH_DISC_TITLE_DATA:
        qDebug()<<"emit receivedSearchTitleDiscData()";
        emit receivedSearchTitleDiscData(Message::getMessageDiscData(message));
        break;
    default:
        qDebug()<<"unknown MSG"<<msgCode;
        qDebug()<<"unknown MSG as hex"<<msgCode.toHex();
    }

}

void TcpConnection::sendMessage(QByteArray data)
{
    qDebug()<<"TcpConnection::sendMessage(QByteArray data):"<<data;
    _server->sendData(data);
}

void TcpConnection::sendReady()
{
    qDebug()<<"TcpConnection::sendReady()";
    sendMessage(Message::getMessage(Message::STATUS_READY));
}

void TcpConnection::sendDone()
{
    qDebug()<<"TcpConnection::sendDone()";
    sendMessage(Message::getMessage(Message::STATUS_DONE));
}

void TcpConnection::sendIdle()
{
    qDebug()<<"TcpConnection::sendIdle()";
    sendMessage(Message::getMessage(Message::STATUS_IDLE));
}

void TcpConnection::sendWorking()
{
    qDebug()<<"TcpConnection::sendWorking()";
    sendMessage(Message::getMessage(Message::STATUS_WORKING));
}

void TcpConnection::sendProgress(int progress)
{
    qDebug()<<"TcpConnection::sendprogress(int progress)";
    sendMessage(Message::getProgressMessage(progress));
}

void TcpConnection::sendDiscData(QList<DiscData> discData)
{
    qDebug()<<"TcpConnection::sendDiscData(QList<DiscData> discData)";
    sendMessage(Message::getDiscDataMessage(discData));
}
void TcpConnection::sendMovieData(QList<MovieData> movieData)
{
    qDebug()<<"TcpConnection::sendMovieData(QList<MovieData> movieData)";
    sendMessage(Message::getMovieDataMessage(movieData));
}
void TcpConnection::sendMovieDataUnknown()
{
    qDebug()<<"TcpConnection::sendMovieDataUnknown()";
    sendMessage(Message::getMessage(Message::MOVIE_DATA_UNKNOWN));
}

void TcpConnection::sendDiscDataUnknown()
{
    qDebug()<<"TcpConnection::sendDiscDataUnknown()";
    sendMessage(Message::getMessage(Message::DISC_DATA_UNKNOWN));
}

void TcpConnection::sendCanceled()
{
    qDebug()<<"TcpConnection::sendCanceled()";
    sendMessage(Message::getMessage(Message::STOP));
}

void TcpConnection::acceptConnection()
{
    qDebug()<<"TcpConnection::acceptConnection()";
    emit acceptedConnection();
}
